<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Permission_Model extends CI_Model{
      function __construct() {
          parent::__construct();
      }
      function get_all_permission(){
          return $this->db->get('permission')->result();
      }
      
      function get_permission_by_id(){
          $ID = $this->uri->segment(3);
          $this->db->where('permission_id',$ID);
          return $this->db->get('permission')->row();
      }
      
      function save(){
          $permission_id = (int)$this->uri->segment(3);
          $data = array(
            'permission_name' => $this->input->post('permission_name'),
            'permission_notice' => $this->input->post('permission_notice')
          );
          
          if($permission_id!=0){
              $this->db->where('permission_id',$permission_id);
              if($this->db->update('permission',$data)){
                  return true;
              }else{
                  return false;
              }
          }else{
              if($this->db->insert('permission',$data)){
                  return true;
              }else{
                  return false;
              }
          }
      }
        function delPermission($id)
        {
            $this->db->where('permission_id',$id);
            $check = $this->db->get('permission')->row();
            if($check){
                 $this->db->where('permission_id',$id);
                 $query=$this->db->get('permission_detail');
                 if($query->num_rows()>0){
                     $rs= $query->result();
                     foreach($rs as $function){
                        $this->db->where('function_id',$function->function_id);
                        if($this->db->delete('permission_detail')){
                            $this->db->where('function_id',$function->function_id);
                            $this->db->delete('permission_admin');
                              //return true;
                        }else{
                              //return false;
                        }
                     }
                 }
                 $this->db->where('permission_id',$id);
                 if($this->db->delete('permission')){
                       return true;
                 }else{
                       return false;
                 }
            }else{
                 return false;
            }
        }
    }
?>
